package com.pks.datacenter.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.entity.api.ApiLog;
import com.pks.datacenter.service.api.ApiLogService;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.UtilString;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/apiLog")
@Scope("prototype")
@Component("apiLogController")
@Api(tags = "接口日志")
public class ApiLogController extends BasicController {

	@Resource
	private ApiLogService apiLogService;


	@ApiOperation(value = "获取详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "id", dataType = "long" , paramType = "query" )
	})
	@RequestMapping(value = "/get" , method = RequestMethod.GET)
	public SRemoteResult<ApiLog> findInfo(HttpServletRequest request, Long id) {
		if (id == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		ApiLog obj = apiLogService.findById(id);
		if (obj == null) {
			return SRemoteResult.initErrorDataNone();
		}
		return SRemoteResult.initSuccess(obj);
	}

	@ApiOperation(value = "列表查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "apiUserId",value = "接口账号ID", dataType = "Long" , paramType = "query" )
		,@ApiImplicitParam(name = "apiUserAccount",value = "接口账号名称", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "apiId",value = "接口ID", dataType = "Long" , paramType = "query" )
		,@ApiImplicitParam(name = "apiMethodName",value = "接口方法名", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "requestIp",value = "请求IP", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "offset",value = "分页起始页（默认0）", dataType = "int" , paramType = "query" )
		,@ApiImplicitParam(name = "pagesize",value = "分页每页行数（默认10）", dataType = "int" , paramType = "query" )
	})
	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public SRemoteResult<List<ApiLog>> list(HttpServletRequest request,Long apiUserId ,String apiUserAccount ,Long apiId ,String apiMethodName ,String requestIp  ,Integer offset,Integer pagesize) {
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		if (apiUserId != null) {
			qbuilder.put("apiUserId", apiUserId);
		}
		if (UtilString.isNotBlank(apiUserAccount)) {
			qbuilder.put("apiUserAccount", apiUserAccount);
		}
		if (apiId != null) {
			qbuilder.put("apiId", apiId);
		}
		if (UtilString.isNotBlank(apiMethodName)) {
			qbuilder.put("apiMethodName", apiMethodName);
		}
		if (UtilString.isNotBlank(requestIp)) {
			qbuilder.put("requestIp", requestIp);
		}
		List<ApiLog> list = new ArrayList<ApiLog>();
		int total = 0;
		total = apiLogService.getTotalRows(qbuilder);
		if (total > 0) {
			list = apiLogService.findByPage(null, qbuilder);
		}
		return SRemoteResult.initPageList(list, total);
	}

}
